﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Framework;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Logger.Business;
using TjZheng.Logger.DataEntity;
using TjZheng.Base;
using TjZheng.CMS.Process.Business;

namespace TjZheng.CMS.Controllers
{
    public class LogCategoryController : CMSController
    {
        BizLogCategory objBizLogCategory { get; set; }
        public LogCategoryController()
        {
            objBizLogCategory = new BizLogCategory();
        }

        public ActionResult LogSet()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LogCategoryIndex()
        {
            return View();
        }
        [QueryMsSqlFilter("ParentId")]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            return Json(objBizLogCategory.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters, "Id"));
        }
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult TreeJson(long id)
        {
            LoggerProcess objLoggerProcess = new LoggerProcess();
            return Json(objLoggerProcess.BindTrees(id));
        }
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult TreeWithOtherJson(long id)
        {
            LoggerProcess objLoggerProcess = new LoggerProcess();
            return Json(objLoggerProcess.BindTreesWithOther(id));
        }
        public ActionResult EditPage(long? id)
        {
            LogCategory objLogCategory = null;
            if (id.HasValue)
            {
                objLogCategory = objBizLogCategory.GetRecord(id.Value);
            }
            else
            {
                objLogCategory = new LogCategory();
            }
            return View(objLogCategory);
        }

        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult LogTypeList()
        {
            BizLogType objBizLogType = new BizLogType();
            var objList = objBizLogType.GetList("");
            InvokeResult objInvokeResult = new InvokeResult();
            if (objList.IsNull())
            {
                objInvokeResult.ResultCode = "DataEmpty";
                return Json(objInvokeResult);
            }
            objInvokeResult.Data = objList.Select(c => new SelectListItem { Text = c.TypeCode, Value = c.TypeCode });
            return Json(objInvokeResult, "text/plain");
        }

        //[ValidateAntiForgeryToken]
        public ActionResult Save(LogCategory objProvide)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            if (objProvide.ParentId <= 0)
            {
                objInvokeResult.ResultCode = "ParentIdNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            LogCategory objTarget = null;
            if (objProvide.Id <= 0)
            {
                objBizLogCategory.ExecuteTransation(() =>
                {
                    objTarget = new LogCategory();
                    Save(objTarget, objProvide);

                    var objParent = objBizLogCategory.GetRecord(objProvide.ParentId);
                    objTarget.Depth = objParent.Depth + 1;
                    objTarget.IDPath = objParent.IDPath + objTarget.Id + ",";
                    objTarget.Id = objBizLogCategory.Add(objTarget);

                    if (objParent.HasChild != 1)
                    {
                        objParent.HasChild = 1;
                        objBizLogCategory.Update(objParent);
                    }
                });
            }
            else
            {
                objTarget = objBizLogCategory.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizLogCategory.Update(objTarget);
            }
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }

        public ActionResult Delete(string id)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizLogCategory.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }

        void Save(LogCategory objTarget, LogCategory objProvide)
        {
            objTarget.CategoryName = objProvide.CategoryName.CutWord(64);
            objTarget.CategoryCode = objProvide.CategoryCode.CutWord(64);
            objTarget.SaveType = objProvide.SaveType.CutWord(16);
            objTarget.ParentId = objProvide.ParentId;
        }
    }
}
